

bt.module({builder: function(bt) {

    var j$ = jQuery;
    var map;
    var clusterer;
    var infowindow;
    var _debug;

    function getData(point) {
        var items = point.children();
        var lat = parseFloat($(items[0]).text());
        var lon = parseFloat($(items[1]).text());
        var content = $(items[2]).text();
        var wp_id = null;
        var city_name = "";
        if (items.length > 3) {
            wp_id = $(items[3]).text();
        }
        if (items.length > 4) {
            city_name = $(items[4]).text();
        }
        return [new google.maps.LatLng(lat, lon), content, wp_id, city_name];
    }

    function initialize() {
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var myOptions = {
            zoom: 8,
            minZoom: 2,
            maxZoom: 16,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        var points = $('#map_data').children();
        var coordinates = [];
        var beingthere = [];

        for (var i = 0; i < points.length; i++) {
            var point = $(points[i]);
            var type = point.attr('class');
            var data = getData(point);
            coordinates.push(data[0]);
            var image = new google.maps.MarkerImage(STATIC_URL+
                    'img/icon_photo.png',
                    new google.maps.Size(35, 37),
                    new google.maps.Point(0, 0),
                    new google.maps.Point(12, 30));
            var marker = new google.maps.Marker({
                position: data[0],
                icon: image
            });
            marker.cityName = data[3];
            beingthere.push(marker);
        }

        var clustererStyles = [];
        var imagePath = STATIC_URL+"img/clusterer";
        var imageExtension = "png";
        var imageSizes = [62, 66, 72, 84, 94];

        for(i = 0; i < 5; i ++) {
            var style = {
                textColor: "white",
                url: imagePath + (i + 1) + "." + imageExtension,
                width: imageSizes[i],
                height: imageSizes[i]
            };
            clustererStyles.push(style);
        }
        clusterer = new MarkerClusterer(map, beingthere, {
            maxZoom: 16,
                  gridSize: 80,
                  styles: clustererStyles,
                  minimumClusterSize: 1,
                  calculator: function(markers) {
                      var index = 0;
                      var count = markers.length;
                      var ladders = [0, 4, 16, 64, 256];
                      while(index < 5 && count > ladders[index]) {
                          index ++;
                      }

                      return {
                          text: count,
                  index: index,
                  title: ""
                      };
                  }
        });
        /*
        google.maps.event.addListener(clusterer, "mouseover", function(data) {
            _debug = data;
            data.getPosition = function() {
                return data.getCenter();
            };
            if(infowindow) {
                var ll = infowindow.getPosition();
                if(ll != data.center_) {
                    infowindow.close();
                } else {
                    return;
                }
            }
            var cities = [];
            var markers = data.markers_;
            for(i in markers) {
                if(markers[i].cityName != "") {
                    cities.push(markers[i].cityName);
                }
            }
            citiesNames = cities.join("    ");
            infowindow = new InfoBox({
                content: citiesNames,
                       maxWidth: 186,
                       pixelOffset: new google.maps.Size(-93, data.clusterIcon_.height_ / -2 - 130),
                       boxStyle: {
                           "background": "rgba(0, 0, 0, 0.8)",
                       "font-size": "12px",
                       "color": "#ffffff",
                       "width": "186px",
                       "height": "130px",
                       "overflow-y": "auto"
                       }
            });
            infowindow.open(map, data);
        });
        */

        /* display the whole world */
        var latlngbounds = new google.maps.LatLngBounds();
        latlngbounds.extend(new google.maps.LatLng(70, -180));
        latlngbounds.extend(new google.maps.LatLng(-70, 180));
        map.fitBounds(latlngbounds);
    }
    $(window).load(function() {
        initialize();
    });

}});
